package com.zy.spring01;

import com.zy.domain.Person;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringTest1
{
    @Test
    public void test1()
    {
        //1.读取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person=(Person)applicationContext.getBean("personId");
        System.out.println(person);
    }

    @Test
    public void test2()
    {
        //使用绝对路径
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("G:\\Users\\Administrator\\Intellij IDEA\\SpringPractice\\SpringTest1\\src\\main\\resources\\applicationContext.xml");
        Person person=(Person)applicationContext.getBean("personId");
        System.out.println(person);
    }

    @Test
    public void test3()
    {
        //1.读取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person1=(Person)applicationContext.getBean("personId");
        Person person2=applicationContext.getBean("personId",Person.class);
        System.out.println(person1==person2);//打印true,即默认为singleton
    }
    @Test
    public void test4()
    {
        //1.读取配置文件
        //2.scope域为singleton
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person1=(Person)applicationContext.getBean("personId");
        Person person2=applicationContext.getBean("personId",Person.class);
        System.out.println(person1==person2);//打印true
    }
    @Test
    public void test5()
    {
        //1.读取配置文件
        //2.scope域为prototype
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person1=(Person)applicationContext.getBean("personId");
        Person person2=applicationContext.getBean("personId",Person.class);
        System.out.println(person1==person2);//打印false
    }
    @Test
    public void test6()
    {
        //1.读取配置文件
        //2.lazy-init=true.懒加载
        //Person对象在使用时即获取Person对象时才被创建
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("之前");
        Person person=(Person)applicationContext.getBean("personId");
        System.out.println(person);
        System.out.println("之后");
    }
    @Test
    public void test7()
    {
        //1.读取配置文件
        //2.lazy-init=false.非懒加载，默认为非懒加载，即false
        //Person对象在使用之前即获取Person对象之前就已经被创建
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("之前");
        Person person=(Person)applicationContext.getBean("personId");
        System.out.println(person);
        System.out.println("之后");
    }

    @Test
    public void test8()
    {
        //1.读取配置文件
        //2.lazy-init=false.非懒加载，默认为非懒加载，即false
        //Person对象在使用之前即获取Person对象之前就已经被创建
        //init()方法被调用
        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        applicationContext.close();//destories()方法被调用
    }
}
